package com.whty.cms.filewps.upload.service.impl;

import cn.hutool.json.JSONObject;
import com.whty.cms.filewps.common.service.impl.BaseAbstractServiceImpl;
import com.whty.cms.filewps.upload.dao.TeamworkLessonMapper;
import com.whty.cms.filewps.upload.dto.TeamworkLesson;
import com.whty.cms.filewps.upload.service.ILessonService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("lessonService")
public class LessonServiceImpl extends BaseAbstractServiceImpl<TeamworkLesson> implements ILessonService {

    @Autowired
    private TeamworkLessonMapper teamworkLessonMapper;

    @Override
    public void save(TeamworkLesson teamworkLesson) {
        teamworkLessonMapper.insert(teamworkLesson);
    }

    @Override
    public List<TeamworkLesson> selectAll(Map<String,String> map) {
        Example example=new Example(TeamworkLesson.class);//要查询的表对应的实体类
        Example.Criteria criteria=example.createCriteria();//创建查询标准
        List<String> list = new ArrayList<>();
        if (StringUtils.isNotBlank(map.get("userId"))) {
            list.add(map.get("userId"));
            if (!"everyone".equals(map.get("userId"))) {
                list.add("everyone");
            }
            criteria.andIn("userId", list);
        }
        if (StringUtils.isNotBlank(map.get("resId"))){
            criteria.andEqualTo("resId",map.get("resId"));
        }
        if (StringUtils.isNotBlank(map.get("status"))){
            criteria.andEqualTo("status",map.get("status"));
        }
        if (StringUtils.isNotBlank(map.get("type"))){
            criteria.andEqualTo("type",map.get("type"));
        }
        if (StringUtils.isNotBlank(map.get("permission"))){
            criteria.andEqualTo("permission",map.get("permission"));
        }
        example.setOrderByClause("create_time desc");
        Integer curPage = null;
        Integer numPerPage = null;
        if (StringUtils.isBlank(map.get("numPerPage"))){
            numPerPage = 10;
        }else {
            numPerPage=Integer.valueOf(map.get("numPerPage"));
        }
        if (StringUtils.isBlank(map.get("curPage"))){
            curPage = 0;
        }else {
            curPage=(Integer.valueOf(map.get("curPage"))-1) *numPerPage;
        }
        RowBounds rowBounds = new RowBounds(curPage, numPerPage);
        return teamworkLessonMapper.selectByExampleAndRowBounds(example,rowBounds);
    }

    @Override
    public void updateRes(TeamworkLesson teamworkLesson) {
        teamworkLessonMapper.updateByPrimaryKeySelective(teamworkLesson);
    }

    @Override
    public void update(JSONObject obj) {
        Example example = new Example(TeamworkLesson.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resId",obj.getStr("resId"));
        TeamworkLesson teamworkLesson = new TeamworkLesson();
        if (StringUtils.isNotBlank(obj.getStr("type"))) {
            teamworkLesson.setType(obj.getStr("type"));
        }
        if (StringUtils.isNotBlank(obj.getStr("status"))) {
            teamworkLesson.setStatus(obj.getStr("status"));
        }
        if (StringUtils.isNotBlank(obj.getStr("permission"))) {
            teamworkLesson.setPermission(obj.getStr("permission"));
        }
        if (StringUtils.isNotBlank(obj.getStr("isEdit"))) {
            teamworkLesson.setIsEdit(obj.getStr("isEdit"));
        }
        if (StringUtils.isNotBlank(obj.getStr("fid"))) {
            teamworkLesson.setFid(obj.getStr("fid"));
        }
        if (StringUtils.isNotBlank(obj.getStr("previewUrl"))) {
            teamworkLesson.setPreviewUrl(obj.getStr("previewUrl"));
        }
        if (StringUtils.isNotBlank(obj.getStr("downloadUrl"))) {
            teamworkLesson.setDownloadUrl(obj.getStr("downloadUrl"));
        }
        if (StringUtils.isNotBlank(obj.getStr("title"))) {
            teamworkLesson.setResName(obj.getStr("title"));
        }
        teamworkLessonMapper.updateByExampleSelective(teamworkLesson,example);
    }

    @Override
    public Integer selectCount(TeamworkLesson teamworkLesson) {
        return teamworkLessonMapper.selectCount(teamworkLesson);
    }

}
